Django whitenoise
whitenoiseとは
wsgiサーバで静的ファイルを効率的に提供できるようにするpythonモジュールのこと
WhiteNoise — WhiteNoise 5.2.0 documentation
結論
nginxなどのwebサーバ無しに、静的ファイルを提供できるって話
基本的な使い方
特別なことが無い限りこの設定で十分
1. pip install whitenoise
2. MIDDLEWAREに、whitenoiseのアプリを追加する
code: settings.py
...
MIDDLEWARE = [
# 'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# ...
]
3. 静的ファイルの保存の仕方をwhitenoiseに任せる。
code: settings.py
...
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
DEBUG = False # False、つまり本番環境でないとエラーが発生する
4. wsgiに渡すアプリケーションインスタンスをwhitenoiseでラップする
code: wsgi.py
from whitenoise import WhiteNoise
from my_project import MyWSGIApp
application = MyWSGIApp()
application = WhiteNoise(application, root='/path/to/static/files')